# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=clucene
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.3.4
pkgrel=5
pkgdesc="CLucene - a C++ search engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://clucene.sourceforge.io/"
license=('spdx:Apache-2.0 OR LGPL-2.1-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost" )
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://downloads.sourceforge.net/project/${_realname}/${_realname}-core-unstable/${pkgver:0:3}/${_realname}-core-${pkgver}.tar.gz"
        clucene-core-2.3.3.4.patch
        002-install-contribs-lib.patch
        003-fix-pkgconfig-file.patch
        004-fix-narrowing-conversions.patch
        005-Replace-std-binary_function-with-typedefs.patch
        010-pthread.patch
        020-gcc.patch)
sha256sums=('ddfdc433dd8ad31b5c5819cc4404a8d2127472a3b720d3e744e8c51d79732eab'
            'a3b0bb664231ed5e66384dd606422d7f6d9f0e6181f295056acfbe0b0d4d7c9c'
            'd3dbb355d00445455fb91c74dacdea380048d3fc2f71b13c5be656ada0f627db'
            'd4e4082dba978f3267d40820b75889606b41a5f639f148bc75067b3d04ee4d14'
            '7828895a3da58ef4e6d2f6480af01ab53579f4fc9984043e3708b8c16431bd0c'
            '6594312b42a9bf9b5130ec22075da006fa1024cbabcacf919cc86e672e1f476c'
            '81f96824bf2ff0dc35288be6fa891df66841110fcd26f59c77025a96b831b637'
            '821471b0bc4e36571eea1d4d637e1c7881aae4ab18a78fed8be29771e38b58b0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-core-${pkgver}"
  apply_patch_with_msg \
    clucene-core-2.3.3.4.patch \
    002-install-contribs-lib.patch \
    003-fix-pkgconfig-file.patch \
    004-fix-narrowing-conversions.patch \
    005-Replace-std-binary_function-with-typedefs.patch \
    010-pthread.patch \
    020-gcc.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_STATIC_LIBRARIES=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DBUILD_CONTRIBS_LIB=ON \
    "${_extra_config[@]}" \
    "../${_realname}-core-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-core-${pkgver}"/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}/${_realname}-core-${pkgver}"/*.license "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
}
